package com.ctgu.yxr;

import java.util.Scanner;

/**
 * Created By Intellij IDEA
 *
 * @author ssssheep
 * @package com.ctgu.yxr
 * @datetime 2022/9/12 星期一
 */
public class App2 {
    public static void main(String[] args) {
        // 输入n个数，存到数组中，找出这个数组中子数组之和的最大值
        int n = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数组的长度：");
        n = scanner.nextInt();
        int[] arr = new int[n];
        System.out.println("请输入数组的元素：");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int max = arr[0];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            if (sum > max) {
                max = sum;
            }
            if (sum < 0) {
                sum = 0;
            }
        }
        System.out.println("最大子数组之和为：" + max);
    }
}
